<template>
  <el-dialog
    :title='成绩录入'
    :close-on-press-escape="false" 
    :close-on-click-modal="false"
    :append-to-body="true"
    :lock-scroll="true"
    :visible.sync="visible">
    <el-form
    label-position="right"  label-width="80px"
    status-icon
    :inline="true"
    size="medium"
    :model="dataForm" :rules="dataRule" ref="dataForm"  @keydown.enter.native.prevent="dataFormSubmit()"  >
      <el-form-item label="学生学号" prop="susername"  >
        <el-input v-model="dataForm.susername" readonly=""></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="stuName"  >
        <el-input v-model="dataForm.stuName" readonly></el-input>
      </el-form-item>
      <el-form-item label="指导成绩" prop="grade"  >
        <el-input v-model="dataForm.grade" placeholder="请输入指导成绩"></el-input>
      </el-form-item>
      <el-form-item label="评阅成绩" prop="grade1"  >
        <el-input v-model="dataForm.grade1" placeholder="请输入评阅成绩"></el-input>
      </el-form-item>
      <el-form-item label="答辩成绩" prop="grade2"  >
        <el-input v-model="dataForm.grade2" placeholder="请输入答辩成绩"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from '@/utils/validate'
import servicesApi from '@/services/servicesApi';
export default {
  data() {

    let quYu = (rule, value, callback) => { // 数字校验
      if (value.length == 0) {
        return callback(new Error('成绩不能为空,没有请填0'));
      }
      if (value < 0) {
        return callback(new Error('成绩不能为负数'));
      }
      if (value > 100) {
        return callback(new Error('成绩不能超过100'));
      }
      if (!(/^[-+]?\d+$/).test(value)) {
        return callback(new Error('请输入正整数或0'));
      }
      if (value.length > 3) {
        callback(new Error('数值长度不能超过3个字符'));
      } else {
        callback();
      }
    };
    return {
      visible: false,
      userId: 0,
      dataForm: {
        susername: '',
        stuName: '',
        grade: null,
        grade1: null,
        grade2: null
      },
      dataRule: {
        grade: [
          { required: true, message: '指导成绩不能为空', trigger: 'blur' },
          {validator: quYu, trigger: 'change' }
        ],
        grade1: [
          { required: true, message: '评阅成绩不能为空', trigger: 'blur' },
          {validator: quYu, trigger: 'change' }
        ],
        grade2: [
          { required: true, message: '答辩成绩不能为空', trigger: 'blur' },
          {validator: quYu, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init(row) {
      this.visible = true
            this.dataForm =  row
        },
    dataFormSubmit() {
      this.$refs['dataForm'].clearValidate() // 移除表单项的校验结果
      this.$refs['dataForm'].validate((valid) => {
            if (!valid) { return false }
          servicesApi.editgradeenterApi(this.dataForm).then(res => {
            let code = res.data.code
            // console.log(code)
            if (code == 200) {
              this.$message({
                message: '录入成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(res.data.msg)
            }
        })
      }) 
    }
  }
}
</script>
